import Basic from "../Basic";

export default class Router extends Basic {
  public router: any;

  public store: any;

  public skipState: string = "NEXT";

  public routerList: any[] = [];

  public routerIndex: number = 0;

  public isBack: boolean  = true;

  public setRouter(router: any) {
    this.router = router;
  }

  public setStore(store: any) {
    this.store = store;
  }

  public initialize() {
    this.addFilter();
  }

  public addFilter() {
    // this.router.beforeEach( (to: any, from: any, next: any) => {
    //   if ( this.isBack ) {
    //     next(false);
    //
    //     return Drive.backButton();
    //   }
    //   setTimeout( () => {
    //     this.isBack = true;
    //   }, 500);
    //   this.isBack = true;
    //   next();
    // });

    this.router.beforeEach((to: any, from: any, next: any) => {
      if ("NEXT" === this.skipState) {
        // 移除后缀路由
        this.routerList.splice(this.routerIndex);
        // 记录路由
        this.routerList.push(from);
        // 移动索引
        this.routerIndex++;
      }
      if ("PREVIOUS" === this.skipState) {
        const routerIndex = this.findRouterIndex(to);
        // 移除后缀路由
        this.routerList.splice(routerIndex);
        // 移动索引
        this.routerIndex = routerIndex;
      }
      this.store.commit("Router/setRouterList", [...this.routerList]);
      this.store.commit("Router/setRouterIndex", this.routerIndex);
      this.store.commit("Router/setSkipState", this.skipState);
      next();
    });

    this.router.beforeEach((to: any, from: any, next: any) => {
      to.meta.title && (document.title = to.meta.title);

      this.store.commit("Global/setDialog", {show: false});
      this.store.commit("Global/setSharePop", {show: false});
      this.store.commit("Global/setPayPop", {show: false, callBack: () => {/**/}});
      next();
    });
  }

  public findRouterIndex(path: any): number {
    for (let i = 0; this.routerList[i]; i++) {
      if (this.routerList[i].path === path.path) {
        return i;
      }
    }
    return this.routerList.length;
  }

  // public addRouter(router: any[]) {
  //   const route = [...router];
  //   // for ( let i = 0; route[i]; i++ ) {
  //   //   route[i].path
  //   // }
  //   return [...router, .]
  public filterOption(options: any) {
    const routes = options.routes;
    const cpRoutes: any[] = [];
    for ( let i = 0; routes[i]; i++ ) {
      cpRoutes.push({...routes[i]});
      routes[i].path += "(|/|/index.html|index.html)";

      if ( routes[i].children ) {
        routes[i].children = this.filterOption({routes: routes[i].children}).routes;
      }
    }

    return {routes: [...routes, ...cpRoutes], mode: process.env.NODE_ENV === "development" ? "hash" : "history"};
  }

  // 下一页
  public next(path: any) {
    this.skipState = "NEXT";
    this.isBack = false;
    const userId: string|number = localStorage.getItem("user_id") || "0";
    // const routerUser = +this.router.currentRoute.query.userId;
    // userId = routerUser || userId;
    const options: any = { path };
    options.query = this.router.currentRoute.query; // { userId }
    // this.router.push(this.autoRouter(path));
    this.router.push(options);
  }

  // 上一页
  public previous(path?: string) {
    this.skipState = "PREVIOUS";
    this.isBack = false;
    // 返回上一页
    if ( !path ) { return this.router.go(-1); }
    // const toPath = path; // this.autoRouter(path);
    // 到指定页面
    let userId: string|number = localStorage.getItem("user_id") || "0";
    const routerUser = +this.router.currentRoute.query.userId;
    userId = routerUser || userId;
    const options: any = { path };
    options.query = { userId };
    // this.router.push(this.autoRouter(path));
    this.router.push(options);
  }

  public autoRouter(path: string): string {
    let toPath     = "";
    if ("/" === path[path.length - 1]) {
      toPath      = `${path}index.html`;
    } else {
      toPath      = `${path}/index.html`;
    }
    return toPath;
  }

  public getMatchedComponents() {
    return this.router.getMatchedComponents();
  }
}
